package gui;
/**
 * Displays the UI elements for the login screen
 */
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import controller.LoginController;

public class LoginDisplay extends Display {
	private LoginController loginController;
	
	private JTextField usernameField;
	private JPasswordField passwordField;
	
	/**
	 * Links the LoginDisplay with a LoginController
	 * @param c Controller to be linked
	 */
	public LoginDisplay(LoginController c) {
		super();
		drawLogin();
		this.loginController = c;
	}
	
	/**
	 * Links the logindisplay with a Controller and a Display
	 * @param c Controller to be linked
	 * @param lastDisplay Display to be linked
	 */
	public LoginDisplay(LoginController c, Display lastDisplay) {
		super(lastDisplay.getFrame());
		drawLogin();
		this.loginController = c;
	}
	
	/**
	 * Draws all the UI elements and buttons for the login screen
	 */
	public void drawLogin() {
		frame.getContentPane().removeAll();
		
		ImagePanel LOGIN = new ImagePanel(new ImageIcon(GUI.class.getResource("/images/Login_Background.png")).getImage());
		LOGIN.setBackground(Color.BLACK);
		frame.getContentPane().add(LOGIN, "");
		LOGIN.setLayout(null);
		
		JLabel lblLoginTitle = new JLabel("");
		lblLoginTitle.setBounds(63, 7, 500, 290);
		LOGIN.add(lblLoginTitle);
		lblLoginTitle.setIcon(new ImageIcon(GUI.class.getResource("/images/Logo.png")));
		lblLoginTitle.setHorizontalAlignment(SwingConstants.CENTER);
		
		JLabel lblUsername = new JLabel("Username:");
		lblUsername.setBounds(160, 325, 51, 15);
		lblUsername.setHorizontalAlignment(SwingConstants.RIGHT);
		LOGIN.add(lblUsername);
		lblUsername.setFont(new Font("Candara", Font.BOLD, 11));
		lblUsername.setForeground(Color.WHITE);
		lblUsername.setLabelFor(usernameField);
		
		usernameField = new JTextField();
		usernameField.setBounds(226, 323, 175, 20);
		LOGIN.add(usernameField);
		usernameField.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(146, 352, 80, 15);
		lblPassword.setHorizontalAlignment(SwingConstants.CENTER);
		LOGIN.add(lblPassword);
		lblPassword.setForeground(Color.WHITE);
		lblPassword.setFont(new Font("Candara", Font.BOLD, 11));
		
		passwordField = new JPasswordField();
		passwordField.setBounds(226, 350, 175, 20);
		LOGIN.add(passwordField);
		
		final JLabel lblPasswordIncorrect = new JLabel("Username or password incorrect.");
		lblPasswordIncorrect.setFont(new Font("Segoe UI", Font.PLAIN, 13));
		lblPasswordIncorrect.setHorizontalAlignment(SwingConstants.CENTER);
		lblPasswordIncorrect.setForeground(Color.RED);
		lblPasswordIncorrect.setBounds(201, 412, 224, 16);
		lblPasswordIncorrect.setVisible(false);
		LOGIN.add(lblPasswordIncorrect);
		
		final JButton btnForgotYourPassword = new JButton("Forgot Your Password?");
		btnForgotYourPassword.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//TODO code for password recovery
			}
		});
		btnForgotYourPassword.setBounds(226, 431, 175, 20);
		btnForgotYourPassword.setVisible(false);
		LOGIN.add(btnForgotYourPassword);
		
		JButton btnLogin = new JButton("Login");
		btnLogin.setBounds(226, 377, 85, 23);
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loginController.login(usernameField.getText(), new String(passwordField.getPassword()));
				
				//TODO if login fails, the following code is activated:
				 
				 lblPasswordIncorrect.setVisible(true);
				 btnForgotYourPassword.setVisible(true);
				 
			}
		});
		LOGIN.add(btnLogin);
		
		JButton btnRegister = new JButton("Register");
		btnRegister.setBounds(316, 377, 85, 23);
		btnRegister.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loginController.register();
			}
		});
		LOGIN.add(btnRegister);
		
		frame.setVisible(true);
	}
}
